{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "22c46502",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Sankey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "be77c6d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "nodes = [\n",
    "    {'name': 'c1'},\n",
    "    {'name': 'c2'},\n",
    "    {'name': 'c3'},\n",
    "    {'name': 'c4'},\n",
    "    {'name': 'c5'},\n",
    "    {'name': 'c6'},\n",
    "    {'name': 'c7'},\n",
    "]\n",
    "\n",
    "links = [\n",
    "    {'source': 'c1', 'target': 'c2', 'value': 10},\n",
    "    {'source': 'c2', 'target': 'c3', 'value': 20},\n",
    "    {'source': 'c3', 'target': 'c4', 'value': 30},\n",
    "    \n",
    "    {'source': 'c5', 'target': 'c6', 'value': 50},\n",
    "    {'source': 'c6', 'target': 'c7', 'value': 60},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ab325813",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"2c29332d88814f9bb5043baaf6865d82\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_2c29332d88814f9bb5043baaf6865d82 = echarts.init(\n",
       "                    document.getElementById('2c29332d88814f9bb5043baaf6865d82'), 'white', {renderer: 'canvas'});\n",
       "                var option_2c29332d88814f9bb5043baaf6865d82 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"sankey\",\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"c1\"\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"c2\"\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"c3\"\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"c4\"\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"c5\"\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"c6\"\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"c7\"\n",
       "                }\n",
       "            ],\n",
       "            \"links\": [\n",
       "                {\n",
       "                    \"source\": \"c1\",\n",
       "                    \"target\": \"c2\",\n",
       "                    \"value\": 10\n",
       "                },\n",
       "                {\n",
       "                    \"source\": \"c2\",\n",
       "                    \"target\": \"c3\",\n",
       "                    \"value\": 20\n",
       "                },\n",
       "                {\n",
       "                    \"source\": \"c3\",\n",
       "                    \"target\": \"c4\",\n",
       "                    \"value\": 30\n",
       "                },\n",
       "                {\n",
       "                    \"source\": \"c5\",\n",
       "                    \"target\": \"c6\",\n",
       "                    \"value\": 50\n",
       "                },\n",
       "                {\n",
       "                    \"source\": \"c6\",\n",
       "                    \"target\": \"c7\",\n",
       "                    \"value\": 60\n",
       "                }\n",
       "            ],\n",
       "            \"left\": \"5%\",\n",
       "            \"top\": \"5%\",\n",
       "            \"right\": \"20%\",\n",
       "            \"bottom\": \"5%\",\n",
       "            \"nodeWidth\": 20,\n",
       "            \"nodeGap\": 8,\n",
       "            \"nodeAlign\": \"justify\",\n",
       "            \"layoutIteration\": 32,\n",
       "            \"orient\": \"horizontal\",\n",
       "            \"draggable\": true,\n",
       "            \"focusNodeAdjacency\": false,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"right\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 0.2,\n",
       "                \"curveness\": 0.6,\n",
       "                \"type\": \"solid\",\n",
       "                \"color\": \"red\"\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u6851\\u57fa\\u56fe\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_2c29332d88814f9bb5043baaf6865d82.setOption(option_2c29332d88814f9bb5043baaf6865d82);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x26cb83b0ca0>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = (\n",
    "    Sankey()\n",
    "    .add(\n",
    "        '',\n",
    "        nodes,  # 所有节点\n",
    "        links,  # 节点之间的链接关系\n",
    "        linestyle_opt=opts.LineStyleOpts(\n",
    "            opacity=0.2,  # 透明度\n",
    "            curve=0.6,  # 曲线幅度 0~1\n",
    "            color='red'\n",
    "        ),\n",
    "        label_opts=opts.LabelOpts(position='right')\n",
    "    )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title='桑基图'))\n",
    ")\n",
    "c.render_notebook()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
